const {AnalyClass} = require('../ClassAnaly');
const {join} = require('path');
const {fetchFiles,tmpClassPathName} = require('../util');
const {appendFileSync,writeFileSync,readFileSync} = require('fs');

const analyPath = [join(__dirname,'./class/src/com/test/controller')];
const tmppath = join(__dirname,'./dist');


const debuggerFlag = false;

async function analy(){
  let allFiles = [];
  for(let i=0;i<analyPath.length;i++){
      allFiles = [...allFiles,...await fetchFiles(analyPath[i])];
  }
  const dist = join(tmppath,tmpClassPathName);
  writeFileSync(dist,'module.exports=[\n');
  allFiles.forEach((f,idx)=>{
      const fContext = readFileSync(f,'utf-8');
      if(!/public\s+(final\s+|abstract\s+)?class/.test(fContext)){
          //接口不作解析
          return;
      }
      const filename = f.match(/[A-Za-z]+\.java$/)[0];
      if(!debuggerFlag||(debuggerFlag&&filename=='ADto.java')){
          console.log("开始解析文件:",f);
          appendFileSync(dist,(idx>0?',':"")+JSON.stringify(AnalyClass(fContext),null,2));
      }
  })
  appendFileSync(dist,'\n];');
}


analy().then(()=>{
  console.log('============全部解析完成=======');
}).catch(err=>{
  console.error("解析文件异常:",err);
})